<template>
  <div class="vux-header" :class="{'fix-header': fixFlag}">
    <div class="vux-header-left">
      <slot name="overwrite-left">
        <transition :name="transition">
          <a class="vux-header-back" v-show="_leftOptions.showBack" @click="onClickBack">{{ typeof _leftOptions.backText === 'undefined' ? '返回' : _leftOptions.backText}}</a>
        </transition>
        <transition :name="transition">
          <div class="left-arrow" @click="onClickBack" v-show="_leftOptions.showBack"></div>
        </transition>
      </slot>
      <slot name="left"></slot>
    </div>
    <h1 class="vux-header-title" @click="$emit('on-click-title')" v-if="!shouldOverWriteTitle">
      <slot>
        <transition :name="transition">
          <span v-show="title">{{title}}</span>
        </transition>
      </slot>
    </h1>
    <div class="vux-header-title-area" v-if="shouldOverWriteTitle">
      <slot name="overwrite-title"></slot>
    </div>
    <div class="vux-header-right">
      <a class="vux-header-more" @click="$emit('on-click-more')" v-if="rightOptions.showMore"></a>
      <slot name="right"></slot>
    </div>
  </div>
</template>

<script>
import Native from '@/service/native'
export default {
  name: 'mxHeader',
  props: {
    leftOptions: Object,
    fixFlag: Boolean,
    title: String,
    transition: String,
    rightOptions: {
      type: Object,
      default () {
        return {
          showMore: false
        }
      }
    }
  },
  beforeMount () {
    if (this.$slots['overwrite-title']) {
      this.shouldOverWriteTitle = true
    }
  },
  computed: {
    _leftOptions () {
      return Object.assign({
        showBack: true,
        preventGoBack: false
      }, this.leftOptions || {})
    }
  },
  methods: {
    onClickBack () {
      if (this._leftOptions.preventGoBack) {
        this.$emit('on-click-back')
      } else {
        Native.api.exit()
      }
    }
  },
  data () {
    return {
      shouldOverWriteTitle: false
    }
  }
}
</script>

<style lang="less">
@import "../../assets/css/base/variable.less";
.vux-header {
  position: relative;
  display: flex;
  justify-content: center;
  padding: 0 0;
  box-sizing: border-box;
  background-color: @theme-color;
  &.fix-header {
    position: fixed;
    width: 100%;
    top: 0;
    left: 0;
  }
}
.vux-header .vux-header-title {
  line-height: 0.44rem;
  text-align: center;
  font-size: 0.18rem;
  font-weight: 400;
  color: @theme-font-color;
  font-family: PingFangSC-Medium;
}
.vux-header-title-area,
.vux-header .vux-header-title {
  margin: 0 0.9rem 0 1.06rem;
  height: 44px;
  color: @theme-font-color;
  line-height: 0.44rem;
  width: auto;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.vux-header .vux-header-title > span {
  display: inline-block;
}
.vux-header .vux-header-left,
.vux-header .vux-header-right {
  position: absolute;
  top: 0.14rem;
  display: block;
  font-size: 0.14rem;
  line-height: 0.2rem;
  color: @theme-font-color;
}
.vux-header .vux-header-left a,
.vux-header .vux-header-left button,
.vux-header .vux-header-right a,
.vux-header .vux-header-right button {
  float: left;
  margin-right: 0.08rem;
  color: @theme-font-color;
}
.vux-header .vux-header-left a:active,
.vux-header .vux-header-left button:active,
.vux-header .vux-header-right a:active,
.vux-header .vux-header-right button:active {
  opacity: 0.5;
}
.vux-header .vux-header-left {
  left: 0.18rem;
}
.vux-header .vux-header-left .vux-header-back {
  padding-left: 0.16rem;
}
.vux-header .vux-header-left .left-arrow {
  position: absolute;
  width: 0.3rem;
  height: 0.3rem;
  top: 50%;
  left: -0.05rem;
  margin-top: -0.15rem;
  &:before {
    content: "";
    position: absolute;
    width: 0.12rem;
    height: 0.12rem;
    border-style: solid;
    border-color: @theme-font-color;
    border-width: 0.02rem 0 0 0.02rem;
    transform: rotate(315deg);
    top: 0.08rem;
    left: 0.07rem;
  }
}
.vux-header .vux-header-right {
  right: 0.15rem;
}
.vux-header .vux-header-right a,
.vux-header .vux-header-right button {
  margin-left: 0.08rem;
  margin-right: 0;
}
.vux-header .vux-header-right .vux-header-more:after {
  content: "\2022\0020\2022\0020\2022\0020";
  font-size: 0.16rem;
}
.vux-header-fade-in-right-enter-active {
  animation: fadeinR 0.5s;
}
.vux-header-fade-in-left-enter-active {
  animation: fadeinL 0.5s;
}
@keyframes fadeinR {
  0% {
    opacity: 0;
    transform: translateX(150px);
  }
  100% {
    opacity: 1;
    transform: translateX(0);
  }
}
@keyframes fadeinL {
  0% {
    opacity: 0;
    transform: translateX(-150px);
  }
  100% {
    opacity: 1;
    transform: translateX(0);
  }
}
</style>
